package rensoft.gwt.client.lang;

import static com.google.gwt.query.client.GQuery.$;

import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.user.client.ui.IsWidget;

import cs.java.lang.Lang;


import rensoft.gwt.client.event.Event;

public class GwtLang extends Lang {

  @SuppressWarnings({"rawtypes", "unchecked"})
  public static <P, A> Event<A> event() {
    return new Event();
  }

  public static void hideAll(IsWidget... widgets) {
    for (IsWidget widget : widgets)
      widget.asWidget().setVisible(false);
  }

  public static void httpGet(String url, RequestCallback callback) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    builder.setCallback(callback);
    try {
      builder.send();
    } catch (RequestException e) {
      callback.onError(null, e);
    }
  }

  public static void setFadeVisible(boolean visible, IsWidget... widgets) {
    for (IsWidget widget : widgets)
      if (visible)
        $(widget.asWidget()).fadeIn();
      else $(widget.asWidget()).fadeOut();
  }

  public static void showAll(IsWidget... widgets) {
    for (IsWidget widget : widgets)
      widget.asWidget().setVisible(true);
  }
}
